package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/design-circular-deque/">641. 设计循环双端队列</a>
 */
public class Solution641 extends BaseSolution {


    public static void main(String[] args) {
        Solution641 solution = new Solution641();
    }
}

class MyCircularDeque {

    final int[] values;
    int front = 0;
    int rear = 0;

    public MyCircularDeque(int k) {
        values = new int[k + 1];
    }

    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        }
        front = (front - 1 + values.length) % values.length;
        values[front] = value;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }
        values[rear] = value;
        rear = (rear + 1) % values.length;
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % values.length;
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }
        rear = (rear - 1 + values.length) % values.length;
        return true;
    }

    public int getFront() {
        return isEmpty() ? -1 : values[front];
    }

    public int getRear() {
        return isEmpty() ? -1 : values[(rear - 1 + values.length) % values.length];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1) % values.length == front;
    }
}